/**
 * @Author: oldTea
 * @CreateTime: 2024-09-24
 * @Description: 代码生成
 * @Version: 1.0
 */
package com.ambition.demo.config;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;

import java.util.Collections;

/**
 * @Author: oldTea
 * @CreateTime: 2024-09-24
 * @Description: 代码生成
 * @Version: 1.0
 */
public class CodeGenerator {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://106.14.6.119:3306/hmdp?useSSL=false&serverTimezone=UTC", "root", "clj0517")
                .globalConfig(builder -> {
                    builder.author("oldTea")// 设置作者
                            // .enableSwagger() // 开启 swagger 模式
                            .outputDir("G:\\idea_project\\练手项目\\clj_demo\\src\\main\\java") ;// 指定输出目录

                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder
                                .parent("com.ambition.demo") // 设置父包名
                                //.moduleName("demo") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "G:\\idea_project\\练手项目\\clj_demo\\src\\main\\resources\\mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->{
                            builder.addInclude("tb_user");// 设置需要生成的表名
                        }
                        //.addTablePrefix("t_", "c_") // 设置过滤表前缀
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}

